//
//  TBRTextPickerC.m
//  TBRentClient
//
//  Created by tonbright on 2018/5/11.
//  Copyright © 2018年 tonbright. All rights reserved.
//

#import "TBRTextPickerC.h"
#import "YXDateHelpObject.h"
@interface TBRTextPickerC ()<UIPickerViewDataSource,UIPickerViewDelegate>


@property (nonatomic,strong)NSMutableArray *dateArray;

@property (nonatomic,strong)NSMutableArray *actualDateArray;//记录包含年月日的时间--避免后面根据月日来计算的时候出现跨年情况的 年份出现bug------------

@property (nonatomic,strong)NSArray *hourArray;
@property (nonatomic,strong)NSArray *minArray;

@property (nonatomic,strong)NSArray *currentTmpDateHourArray;//当前日期小时推后两个小时---如果分钟数在50之后就要推迟三个小时---------不然没法选择整点----

@property (nonatomic,strong)NSArray *hourTmpArray;//普通

@property (nonatomic,strong)NSArray *currentTmpDateMinArray;//当前分钟数组----根据当前分钟之后
@property (nonatomic,strong)NSArray *minTmpArray;

@property (nonatomic,copy)NSString *currentDateStr;

//是否选中第一行呢
@end

@implementation TBRTextPickerC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.pickerView.dataSource = self;
    self.pickerView.delegate = self;
    self.dateArray = [[YXDateHelpObject manager] latelyTime:60];
    self.actualDateArray = [[YXDateHelpObject manager] latelyTimeYearMonthDay:60];
    NSString *currentHour = [[YXDateHelpObject manager] getStrFromDateFormat:@"HH" Date:[NSDate date]];
    NSString *currentMin = [[YXDateHelpObject manager] getStrFromDateFormat:@"mm" Date:[NSDate date]];
    NSArray *minArray = [NSArray array];
    int firstHour = currentHour.intValue;
    if (currentMin.intValue >= 50) {
        firstHour++;
    }
    NSMutableArray *hourArray = [NSMutableArray array];
    for (int i = firstHour + 2; i < 24; i++) {
        NSString *iStr = [NSString stringWithFormat:@"%d",i];
        [hourArray addObject:iStr];
        
    }
    _currentTmpDateHourArray = hourArray;
    
    
    // MARK: - 修复9点50后小时数组为空的bug
    
    NSMutableArray *tmpAllHourArray = [NSMutableArray array];
    for (int i = 0; i < 24; i++) {
        NSString *iStr = [NSString stringWithFormat:@"%d",i];
        [tmpAllHourArray addObject:iStr];
    }
    _hourTmpArray = tmpAllHourArray;
    
    if (currentMin.intValue >= 0 && currentMin.intValue < 10) {
        minArray = @[@"10",@"20",@"30",@"40",@"50"];
    }else if (currentMin.intValue >= 10 && currentMin.intValue < 20) {
        minArray = @[@"20",@"30",@"40",@"50"];
    }else if (currentMin.intValue >= 20 && currentMin.intValue < 30) {
        minArray = @[@"30",@"40",@"50"];
    }else if (currentMin.intValue >= 30 && currentMin.intValue < 40) {
        minArray = @[@"40",@"50"];
    }else if (currentMin.intValue >=40 && currentMin.intValue < 50) {
        minArray = @[@"50"];
    }else {
        //分钟为50之后自动推迟一个小时--------------------没法选择整点数据了
        minArray = @[@"00",@"10",@"20",@"30",@"40",@"50"];
    }
    _currentTmpDateMinArray = minArray;
    _minTmpArray = @[@"00",@"10",@"20",@"30",@"40",@"50"];
    
    _hourArray = _currentTmpDateHourArray;
    _minArray = _currentTmpDateMinArray;
    
    if (_currentTmpDateHourArray.count == 0) {
        _hourArray = _hourTmpArray;
        [_dateArray removeObjectAtIndex:0];
        [_actualDateArray removeObjectAtIndex:0];
    }
    
    for (int i = 0; i <= 2; i++) {
        [self pickerView:_pickerView didSelectRow:0 inComponent:i];
    }
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        return self.dateArray.count;
    }else if (component == 1) {
        return self.hourArray.count;
    }
    return self.minArray.count;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    if (component == 0) {
        return Width / 2;
    }else {
        return Width / 4;
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 40;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (component == 0) {
        if (row == 0) {
            if (_currentTmpDateHourArray.count == 0) {
                return @"明天";
            }
            return @"今天";
        }else if (row == 1) {
            if (_currentTmpDateHourArray.count == 0) {
                return @"后天";
            }
            return @"明天";
        }else if (row == 2) {
            if (_currentTmpDateHourArray.count == 0) {
                return self.dateArray[row];
            }
            return @"后天";
        }
        return self.dateArray[row];
    }else if (component == 1) {
//        NSString *iStr = [NSString stringWithFormat:@"%d点",i];
        return [NSString stringWithFormat:@"%@点",self.hourArray[row]];//self.hourArray[row];
    }
    return [NSString stringWithFormat:@"%@点",self.self.minArray[row]];//self.minArray[row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) {
        if (row == 0) {
            self.hourArray = self.currentTmpDateHourArray;
            if (_currentTmpDateHourArray.count == 0) {
                self.hourArray = self.hourTmpArray;
            }
            self.minArray = self.currentTmpDateMinArray;
            [self.pickerView reloadComponent:1];
            [self.pickerView reloadComponent:2];
        }else {
            self.hourArray = self.hourTmpArray;
            self.minArray = self.minTmpArray;
            [self.pickerView reloadComponent:1];
            [self.pickerView reloadComponent:2];
        }
    }
    NSInteger dateIndex = [self.pickerView selectedRowInComponent:0];
    if (dateIndex == 0 && component == 1) {
        if (row == 0) {
            self.minArray = self.currentTmpDateMinArray;
            [self.pickerView reloadComponent:2];
        }else {
            self.minArray = self.minTmpArray;
            [self.pickerView reloadComponent:2];
        }
    }
    if (dateIndex != 0) {
        self.minArray = self.minTmpArray;
        [self.pickerView reloadComponent:2];
    }
}


- (IBAction)cancleBtnClick:(UIButton *)sender {
    
    if (sender.tag == 10) {
        
        if (self.pickerViewCancleBtnBlock) {
            self.pickerViewCancleBtnBlock();
        }
    }else if (sender.tag == 20) {
        NSInteger dateIndex = [self.pickerView selectedRowInComponent:0];
        NSInteger hourIndex = [self.pickerView selectedRowInComponent:1];
        NSInteger minIndex = [self.pickerView selectedRowInComponent:2];
        NSLog(@"%ld-----%ld----%ld",dateIndex,hourIndex,minIndex);
        if (self.pickerViewSureBtnBlock) {
            //吧年月日保存起来吗?---------
            NSString *yearStr = self.actualDateArray[dateIndex];
            NSString *dateStr = self.dateArray[dateIndex];
            NSString *hourStr = self.hourArray[hourIndex];
            NSString *minStr = self.minArray[minIndex]; self.pickerViewSureBtnBlock(dateStr,hourStr,minStr);
            
            [TBRRentAllMessageModel shareAllMessageModel].onCarTimeActual = [NSString stringWithFormat:@"%@ %@:%@",yearStr,hourStr,minStr];
        }
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if (self.pickerViewCancleBtnBlock) {
        self.pickerViewCancleBtnBlock();
    }
}

@end
